<?php

namespace App\Filament\Widgets;

use App\Models\BorrowRecord;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Carbon;
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;
class BorrowChart extends ChartWidget
{
    protected static ?string $heading = '借阅';
    protected static ?int $sort = 3;
    protected static ?string $maxHeight = '300px';
    protected int | string | array $columnSpan = 'full';



    protected function getData(): array
    {
        $data = Trend::model(BorrowRecord::class)
            ->between(
                start: now()->subDays(7),
                end: now(),
            )
            ->perDay()
            ->count();
        return [
            //
            'datasets' => [
                [
                    'label' => '借阅数',
                    'data' => $data->map(fn (TrendValue $value) => $value->aggregate),
                    'backgroundColor'=> [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(255, 159, 64, 0.2)',
                        'rgba(255, 205, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(201, 203, 207, 0.2)'
                    ],
                    'borderColor'=>[
                            'rgb(255, 99, 132)',
                            'rgb(255, 159, 64)',
                            'rgb(255, 205, 86)',
                            'rgb(75, 192, 192)',
                            'rgb(54, 162, 235)',
                            'rgb(153, 102, 255)',
                            'rgb(201, 203, 207)'
                        ]
                ],
            ],
            'labels' => $data->map(fn (TrendValue $value) => Carbon::make($value->date)->format('m-d')),
        ];
    }

    protected function getType(): string
    {
        return 'line';
    }
}
